const pomelo = window.pomelo;
let connHost;   //connector地址
let connPort;   //connector端口
let isConnectorEntry=false;    //是否连接Connector
const testUid = "test2"

//用户登陆
async function login(elem) {
  let route = "login.loginHandler.login";

  if(isConnectorEntry){   //重新登陆
    pomelo.disconnect();
    isConnectorEntry = false;
  }

  if(!isConnectorEntry)   //初始连接
    await connectorEntry(document.getElementById("connBtn"));

  //首先初始化connector地址
  return new Promise( resolve => {
    pomelo.request(route, {} , async data => {
      if(data.code !== 200) return;
      showLog("用户登陆成功");
      console.log(data);
      elem.setAttribute("value", "重新登陆");
      resolve();
    });
  });
}

//-------------------------------------

//深度升级
async function depthUpgrade(){
  let route = "upgrade.upgradeHandler.depthUpgrade";
  return new Promise( resolve => {
    pomelo.request(route, {} , async data => {
      showLog("深度升级");
      //depthGrade 当前深度等级  depthNum 当前深度  incomeRate 采集收入速度
      showLog(JSON.stringify(data));
      if(data.code !== 200) return;
      resolve();
    });
  });
}

//品质升级
async function qualityUpgrade(){
  let route = "upgrade.upgradeHandler.qualityUpgrade";
  return new Promise( resolve => {
    pomelo.request(route, {} , async data => {
      showLog("品质升级");
      showLog(JSON.stringify(data));
      if(data.code !== 200) return;
      resolve();
    });
  });
}

//速度升级
async function speedUpgrade(){
  let route = "upgrade.upgradeHandler.speedUpgrade";
  return new Promise( resolve => {
    pomelo.request(route, {} , async data => {
      showLog("速度升级");
      showLog(JSON.stringify(data));
      if(data.code !== 200) return;
      resolve();
    });
  });
}

//领取离线奖励
async function getLogoutCoins(){
  let route = "upgrade.upgradeHandler.getLogoutCoins";
  return new Promise( resolve => {
    pomelo.request(route, {} , data => {
      showLog("领取离线奖励");
      showLog(JSON.stringify(data));
      if(data.code !== 200) return;
      resolve();
    });
  });
}

//双倍领取离线奖励
async function getDoubleLogoutCoins(){
  let route = "upgrade.upgradeHandler.getDoubleLogoutCoins";
  return new Promise( resolve => {
    pomelo.request(route, {} , data => {
      showLog("双倍领取离线奖励");
      showLog(JSON.stringify(data));
      if(data.code !== 200) return;
      resolve();
    });
  });
}

//监听pomelo事件
window.onload = function(){
  //刷新当前金币
  pomelo.on("now_coins",function(msg){
    showLog("更新当前金币数量");
    showLog(JSON.stringify(msg));
  });
}

//-------------------------------------

//连接Connector
async function connectorEntry(elem) {
  let route = "connector.entryHandler.entry";
  if(!connHost || !connPort) 
    await gateConnector(document.getElementById("gateBtn"));
  //首先初始化connector地址
  return new Promise( resolve => {
    pomelo.init({ host: connHost, port: connPort, log: true }, function() {
      pomelo.request(route, { uid: testUid } , data => {
        showLog("初始化connector成功");
        elem.setAttribute("disabled","");
        isConnectorEntry = true;
        resolve();
      });
    });
  });
}

//Gate获取Connector端口地址
function gateConnector(elem) {
  let route = "gate.gateHandler.entry";
  let gateHost = "127.0.0.1";
  let gatePort = 3110;
  return new Promise( resolve => {
    pomelo.init({ host: gateHost, port: gatePort, log: true }, function(err) {
      pomelo.request(route, { uid: testUid } , data => {
        showLog("Gate获取Connector端口地址");
        showLog(JSON.stringify(data));
        if(!data || data.code != 200) return;
        elem.setAttribute("disabled","");
        connHost = data.host;
        connPort = data.port;
        resolve();
      });
    });
  });
}

//显示日志
function showLog(log){
  let logPanel = document.getElementById("showLog");
  let textobj=document.createTextNode(log+"\n");
  logPanel.prepend(textobj);
}
